package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.feign.wemedia.IWmuserClient;
import com.heima.model.apuser.dtos.AuthDto;
import com.heima.model.apuser.pojos.ApUserRealname;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.service.ApUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author rookie001
 * @creat 2023-06-20-14:24
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    //查询用户
    @Override
    public ResponseResult listUser(AuthDto dto) {
        //数据校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页判断
        dto.checkParam();
        //设置分页查询条件
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper();
        //根据审核条件查询
        if (dto.getStatus() != null) {
            lambdaQueryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        //按照注册时间倒叙查询
        lambdaQueryWrapper.orderByDesc(ApUserRealname::getCreatedTime);
        //封装分页
        page = page(page, lambdaQueryWrapper);
        //返回结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;


    }

    @Autowired
    private ApUserService apUserService;
    @Autowired
    private IWmuserClient iWmuserClient;
    //审核通过
    @Override
    public ResponseResult authPass(AuthDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getId() != null) {
            ApUserRealname apUserRealname = getById(dto.getId());
            apUserRealname.setStatus((short) 9);
            updateById(apUserRealname);
            //同步更新apuser表
            apUserService.logout(apUserRealname.getUserId());
            //todo 同步到wmuser 生成自媒体账号
            //查询apuser用户
            ApUser apUser = apUserService.getById(dto.getId());
            iWmuserClient.saveUser(apUser);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
